using System.Collections.Generic;

namespace VerwInfo.Record
{
    internal class RecordLayout
    {
        private static RecordLayout _instance;
        private readonly Dictionary<long, RecordType> _codeToType;
        private readonly Dictionary<RecordType, FieldLayout> _layout;

        private RecordLayout()
        {
            _layout = new Dictionary<RecordType, FieldLayout>
                          {
                              {
                                  RecordType.Batchsluitrecord,
                                  new FieldLayout
                                      {
                                          new Field(FieldName.Recordcode, FieldType.TypeX, 3, "990"),
                                          new Field(FieldName.Aantalpostengeweigerd, FieldType.TypeX, 7),
                                          new Field(FieldName.Aantalpostenteruggebogen, FieldType.TypeX, 7),
                                          new Field(FieldName.Aantalposten, FieldType.TypeX, 7),
                                          new Field(FieldName.Totaalbedrag, FieldType.TypeX, 18),
                                          new Field(FieldName.Filler, FieldType.TypeX, 8, ""),
                                      }
                                  },
                              {
                                  RecordType.Bestandssluitrecord,
                                  new FieldLayout
                                      {
                                          new Field(FieldName.Recordcode, FieldType.TypeX, 3, "990"),
                                          new Field(FieldName.Aantalbatches, FieldType.TypeX, 6),
                                          new Field(FieldName.Vervolgbestandsnummer, FieldType.TypeX, 2),
                                          new Field(FieldName.Filler, FieldType.TypeX, 39, "")
                                      }
                                  },
                              {
                                  RecordType.Bestandsvoorlooprecord,
                                  new FieldLayout
                                      {
                                          new Field(FieldName.Recordcode, FieldType.TypeX, 3, "010"),
                                          new Field(FieldName.Bestandsnaam, FieldType.TypeX, 8),
                                          new Field(FieldName.Filler, FieldType.TypeX, 3, "A"),
                                          new Field(FieldName.Bestandsversie, FieldType.TypeX, 3, "4.1"),
                                          new Field(FieldName.Aanmaakdatum, FieldType.TypeX, 6),
                                          new Field(FieldName.RunnummerBestand, FieldType.TypeX, 4),
                                          new Field(FieldName.RekeningnummerEindbestemming, FieldType.TypeX, 10),
                                          new Field(FieldName.Uitvoervolgnummer, FieldType.TypeX, 4),
                                          new Field(FieldName.Bestandsvolgnummer, FieldType.TypeX, 2),
                                          new Field(FieldName.Filler, FieldType.TypeX, 9, "")
                                      }
                                  },
                              {
                                  RecordType.Omschrijvingsrecord,
                                  new FieldLayout
                                      {
                                          new Field(FieldName.Recordcode, FieldType.TypeX, 3, "110"),
                                          new Field(FieldName.Omschrijving, FieldType.TypeX, 32),
                                          new Field(FieldName.Filler, FieldType.TypeX, 15)
                                      }
                                  },
                              {
                                  RecordType.Woonplaatsrecord,
                                  new FieldLayout
                                      {
                                          new Field(FieldName.Recordcode, FieldType.TypeX, 3, "515"),
                                          new Field(FieldName.Woonplaats, FieldType.TypeX, 35),
                                          new Field(FieldName.Filler, FieldType.TypeX, 12, "")
                                      }
                                  }
                          };
            _codeToType = new Dictionary<long, RecordType>();
            foreach (var definition in _layout)
            {
                _codeToType.Add(definition.Value.Recordcode, definition.Key);
            }
        }

        public static RecordLayout Instance
        {
            get { return _instance ?? (_instance = new RecordLayout()); }
        }

        public RecordType this[long key]
        {
            get { return _codeToType[key]; }
        }

        public FieldLayout this[RecordType key]
        {
            get { return _layout[key]; }
        }

        public bool ContainsKey(long key)
        {
            return _codeToType.ContainsKey(key);
        }
    }
}